package com.whut.transportation.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.whut.transportation.R;
import com.whut.transportation.TransInfoActivity;
import com.whut.transportation.entity.Message;
import com.whut.transportation.entity.TransInfo;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity.DataCallback;
import com.whut.transportation.utils.Constant;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 运输数据绑定适配器
 */
public class TransinfoAdapter extends BaseAdapter {
	private List<TransInfo> traninfos;// 要绑定的数据
	public static List<Boolean> mcheck;
	private LayoutInflater inflater;// 布局填充器
	private Context mContext;
	private String account;
	private String name;
	private MyClickListener mListener;
	private HashMap<Integer, View> mView = new HashMap<Integer, View>(); // 用于保存本次选择的条目
	
	public interface MyClickListener{
		public boolean clickListener(View v,Integer tag,int i);
	}
	
	public TransinfoAdapter(Context context,List<TransInfo> transInfos,MyClickListener mListener,String account,String name){
		this.mContext = context;
		this.traninfos = transInfos;
		this.mListener = mListener;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mcheck = new ArrayList<Boolean>();
		for(int i=0;i<transInfos.size();i++){
			mcheck.add(false);
		}
		this.account = account;
		this.name = name;
	}
	
	@Override
	public int getCount() {
		return traninfos.size();
	}

	@Override
	public Object getItem(int arg0) {
		return traninfos.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null; // 自定义结构体
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.activity_detail_transinfo_item, null);
			holder = new ViewHolder();
			holder.transinfo_number = (TextView) convertView.findViewById(R.id.transinfo_number);
			holder.transinfo_trainNumber = (TextView) convertView.findViewById(R.id.transinfo_trainNumber);
			holder.transinfo_train = (TextView) convertView.findViewById(R.id.transinfo_train);
			holder.transinfo_station = (TextView) convertView.findViewById(R.id.transinfo_station);
			holder.transinfo_time = (TextView) convertView.findViewById(R.id.transinfo_time);
			holder.transinfo_goods = (TextView) convertView.findViewById(R.id.transinfo_goods);
			holder.tips = (TextView) convertView.findViewById(R.id.tips);
			holder.cBox= (CheckBox) convertView.findViewById(R.id.check);
			holder.begin_baodian = (Button) convertView.findViewById(R.id.begin_baodian);
			holder.end_baodian = (Button) convertView.findViewById(R.id.end_baodian);
			holder.enter_op = (Button) convertView.findViewById(R.id.enter_op);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		TransInfo transInfo = traninfos.get(position);
		holder.transinfo_number.setText(transInfo.getNumber());
		holder.transinfo_trainNumber.setText(transInfo.getTrainNumber());
		holder.transinfo_train.setText(transInfo.getTrain());
		holder.transinfo_station.setText(transInfo.getStation());
		holder.transinfo_goods.setText(transInfo.getGoods());
		holder.transinfo_time.setText(transInfo.getTime());
		if(transInfo.getFlag()==1){//第一次检查完成
			holder.tips.setText("车辆第一次检查完成，请进行开始报点");
			holder.tips.setBackgroundColor(Color.YELLOW);
		} else if(transInfo.getFlag()==2){//装卸车信息输入完成
			holder.tips.setText("开始报点完成，请进行完了报点");
			holder.tips.setBackgroundColor(Color.GREEN);
		} else if(transInfo.getFlag()==3){//装卸车信息输入完成
			holder.tips.setText("完了报点完成，请进行第二次车辆检查");
			holder.tips.setBackgroundColor(Color.LTGRAY);
		}else if(transInfo.getFlag() == 5) {//车辆不可维修
			holder.tips.setText("车辆不可维修");
			holder.tips.setBackgroundColor(Color.RED);
		} else if(transInfo.getFlag() == 0){
			holder.tips.setText("车辆未检查");
			holder.tips.setBackgroundColor(Color.GRAY);
		} else if(transInfo.getFlag() == 4){
			holder.tips.setText("所有操作已完成");
			holder.tips.setBackgroundColor(Color.BLUE);
		}
		holder.cBox.setChecked(mcheck.get(position));
		holder.cBox.setTag(position);
		holder.begin_baodian.setTag(position);
		holder.end_baodian.setTag(position);
		holder.enter_op.setTag(position);
		holder.cBox.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Integer tag = (Integer) v.getTag();
				if(mcheck.get(tag)){
					mcheck.set(tag, false);
				} else {
					mcheck.set(tag, true);
				}
				notifyDataSetChanged();
			}
		});
		holder.begin_baodian.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Integer tag = (Integer) v.getTag();
				if(mListener.clickListener(v,tag,0)){
					traninfos.get(tag).setFlag(traninfos.get(tag).getFlag()+1);
					notifyDataSetChanged();
				}
			}
		});
		
		holder.end_baodian.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Integer tag = (Integer) v.getTag();
				if(mListener.clickListener(v,tag,1)){
					traninfos.get(tag).setFlag(traninfos.get(tag).getFlag()+1);
					notifyDataSetChanged();
				}
			}
		});
		holder.enter_op.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Integer tag = (Integer) v.getTag();
				mListener.clickListener(v,tag,2);
			}
		});
		return convertView;
	}
	
	public List<Boolean> getMcheck() {
		return mcheck;
	}
	

}
